<?php

class Form_Register extends Zend_Form
{

    public function init()
    {
        $this->setName('Register');
        $this->setMethod('post');
        
        $email = new Zend_Form_Element_Text('email');
        $email->setLabel('Email:')
        		->setRequired()
        		->addValidator('EmailAddress')
        		->addValidator('StringLength', false, array('max' => 160))
        		->addFilter('StringTrim')
        		->addFilter('StripTags');
        		
        $password = new Zend_Form_Element_Password('password');
        $password->setLabel('Password:')
        		->setRequired()
        		->addValidator('StringLength', false, array('min' => 6, 'max' => 40))
        		->addFilter('StringTrim')
        		->addFilter('StripTags');
        		
        $confirm = new Zend_Form_Element_Password('confirm');
        $confirm->setLabel('Confirm password:')
        		->setRequired()
        		->setIgnore(true)
        		->addValidator('Identical', false, array('token' => 'password'))
        		->addFilter('StringTrim')
        		->addFilter('StripTags');
        		
        $submit = new Zend_Form_Element_Submit('register');
        $submit->setLabel('Register');
        
        $this->addElements(array($email,$password,$confirm,$submit));
    }

    public function persistData($data){
    	$user = new Model_User();
    	$user->email = $data['email'];
    	$user->salt = sha1(uniqid(null,true));
    	$user->password = sha1($data['password'] . $user->salt);
    	$user->save();
    	
    	$config = Zend_Registry::get('config');
    	$url = $config->site->baseurl . '/user/confirm/id/' . $user->id . '/token/' . md5($user->id . $user->email . $user->salt);
    	
    	$mailer = new App_Mailer();
    	$mailer->addTo($user->email)
    		->setSubject('Confirm your registration')
    		->setViewParam('url',$url)
    		->sendHtmlTemplate('confirmregister.phtml');
    }

}

